/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.OnepageImpl;
import com.lcfgly.Dao.Product;
import com.lcfgly.Dao.UserMenuImple;
import com.lcfgly.Dao.UserconuntImpl;
import com.lcfgly.Database.ShopOnepage;
import com.lcfgly.Database.ShopUsermenu;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.SQLEmptyException;
import com.lcfgly.Plug.DataTableForShoprice;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author lcfgly
 */
@Service
public class UserMenuService {
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    UserMenuImple userMenuImple;
    
    @Autowired
    OnepageImpl onepageImpl;
    
    @Autowired
    Product product;
    
    @Autowired
    UserconuntImpl userinferImpl;
    
    @Autowired
    DataTableForShoprice dataTable;
    
    /**
     * 获取栏目
     * @param id
     * @return 
     */
    public List<Map> GetSimpleUserMenu(int id)
    {
        try {
            return userMenuImple.findSimpleByUserid(id);
        } catch (SQLException ex) {
            log.error(ex);
            return null;
        }
    }
    
    /**
     * 获取栏目列表（DataTable插件）数据
     * @param request
     * @param user
     * @return
     * @throws ActionException 
     */
    public Map GetMenuListForDataTable(HttpServletRequest request,UserInfo user) throws ActionException
    {
        try{
            return dataTable.GetDataForDataTable(request, "shop_usermenu", "id,typename,isnews",null, "userid="+user.getUid(), null,null);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 添加表单
     * @param menu
     * @param user
     * @throws ActionException 
     */
    public void menuadd(ShopUsermenu menu,UserInfo user) throws ActionException
    {
        menu.setUserid(user.getUid());
        try{
            int x = userMenuImple.insertBackID(menu);
            if (!menu.getIsnews()) {
                ShopOnepage onepage = new ShopOnepage();
                onepage.setTitle(menu.getTypename());
                onepage.setUpdatetime(new Date(new Date().getTime()));
                onepage.setWriter(user.getUsername());
                onepage.setWriteruid(user.getUid());
                onepage.setTid(x);
                try {
                    onepageImpl.insert(onepage);
                } catch (SQLException e) {
                    userMenuImple.DeleteById(x);
                    log.error(e);
                    throw new ErrorException();
                }
            }
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取产品栏目
     * @return 
     */
    public List GetProductType()
    {
        try{
            return product.GetArctype();
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
    /**
     * 获取栏目信息
     * @param userid 用户ID
     * @param id  栏目ID
     * @return
     * @throws ActionException 
     */
    public Map FindSimpleByIdAndUserId(int userid,int id) throws ActionException{
        try{
            return userMenuImple.findSimpleById(id, userid);
        }catch(SQLEmptyException e){
            log.error(e);
            throw new ActionException("未找到先关数据!");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    } 
    
    /**
     * 按照ID和Userid删除栏目
     * @param id
     * @param userid
     * @return 
     */
    public boolean MenuDelByIdAndUserId(int id,int userid){
        try{
            return userMenuImple.DeleteByIdAndUserid(id, userid)==1;
        }catch(SQLException e){
            log.error(e);
            return false;
        }
    }
    
    /**
     * 获取用户栏目
     * @param id 栏目ID
     * @param userid 用户ID
     * @return
     * @throws ActionException 
     */
    public ShopUsermenu GetMenuById(int id,int userid)throws ActionException{
        try{
            return userMenuImple.findByIdAndUserid(id, userid);
        }catch(SQLEmptyException e){
            log.error(e);
            throw new ActionException("未查找到先关数据! ");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 更新数据
     * @param usermenu
     * @param userid
     * @return
     * @throws ActionException 
     */
    public boolean UpdateWithUserId(ShopUsermenu usermenu,int userid)throws ActionException{
        try{
            usermenu.setUserid(userid);
            return userMenuImple.updateWithUserid(usermenu, userid)==1;
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取简单的用户信息
     * @param id
     * @return 
     */
    public Map GetSimpleUserInfer(int id){
        try{
            return userinferImpl.findSimpleById(id);
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
}
